package com.oceansoft.core.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.dao.IDictionaryDao;
import com.oceansoft.core.domain.Dict;
import com.oceansoft.core.service.IDictionaryService;

@Service
public class DictionaryServiceImp implements IDictionaryService {

	@Resource
	private IDictionaryDao dictionaryDao;

	@Override
	public Pagination<Dict> findByPage(int pageIndex, Dict dict) {
		return dictionaryDao.findByPage(pageIndex, dict);
	}

	@Override
	public List<Dict> findName() {
		return dictionaryDao.findName();
	}

	@Override
	public int saveAddDict(Dict dict) {
		Dict dict1 = dictionaryDao.find(dict.getParentId(), dict.getName());
		if (dict1 != null)
			return 1;
		dictionaryDao.addDict(dict);
		return 0;
	}

	@Override
	public void saveDelete(long dbId) {
		dictionaryDao.delete(dbId);
	}

	@Override
	public int saveEdit(Dict dict) {
		Dict dict1 = dictionaryDao.find(dict.getParentId(), dict.getName());
		if (dict1 != null && dict1.getDbId() != dict.getDbId())
			return 1;
		dictionaryDao.edit(dict);
		return 0;
	}

	@Override
	public List<Dict> findByParentId(long parentId) {
		return dictionaryDao.findByParentId(parentId);
	}

	@Override
	public Dict findByDbId(long dbId) {
		return dictionaryDao.findByDbId(dbId);
	}

	@Override
	public List<Dict> findValues(long dbId) {
		return dictionaryDao.findValues(dbId);
	}
	
	@Override
	public List<Dict> findSysDictValues(String lb,String type) {
		return dictionaryDao.findSysDictValues(lb,type);
	}
	
	@Override
	public List<Dict> findSysDictValues(String type) {
		return dictionaryDao.findSysDictValues(type);
	}

	@Override
	public Map<String, String> findDictsByDictType(String dictType) {
		return dictionaryDao.findDictsByDictType(dictType);
	}

	@Override
	public int findParentId(int dbid) {
		return dictionaryDao.findParentId(dbid);
	}

	@Override
	public LinkedHashMap<String, String> findGCDictItem() {
		return dictionaryDao.findGCDictItem();
	}

	@Override
	public LinkedHashMap<String, Dict> findGCDict() {
		return dictionaryDao.findGCDict();
	}

	@Override
	public Map<String, String> findJZDict() {
		Map<String, Dict> map = dictionaryDao.findJZDict();
		Iterator<String> itor = map.keySet().iterator();
		Map<String, String> rt = new HashMap<String, String>();
		while (itor.hasNext()) {
			Dict d = map.get(itor.next());
			rt.put(d.getName(), String.valueOf(d.getDbId()));
		}
		return rt;
	}

	@Override
	public LinkedHashMap<String, Dict> _findGCDict() {
		return dictionaryDao._findGCDict();
	}

	@Override
	public List<Dict> findBzlbValues(long dbId, long jm) {
		return dictionaryDao.findValues(dbId,jm);
	}

	@Override
	public List<Dict> findDictValues(long dbId, int type) {
		return dictionaryDao.findDictValues(dbId,type);
	}

	@Override
	public boolean checkcode(long parentid, String code) {
		// TODO Auto-generated method stub
		int num = dictionaryDao.checkcode(parentid,code);
		boolean result;
		if(num>0){
			result=false;			
		}else{
			result=true;
		}
		return result;
	}

	@Override
	public String getMaxCode(long parentid) {
		// TODO Auto-generated method stub
		int num =  dictionaryDao.getMaxCode(parentid);
		return String.valueOf(num+1);
	}

}
